﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsConsultantsInformationTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// ConsultantsInformation Business Object
    /// </summary>

    public class ConsultantsInformation : BOBase
    {

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the ConsultantsInformation.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;

                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;


        /// <summary>
        /// YearsofExp.
        /// </summary>
        public int YearsofExp
        {
            get { return _yearsofExp; }
            set
            {
                if (_yearsofExp != value)
                {
                    if (OnPropertyChanging("YearsofExp", _yearsofExp, value))
                    {
                        int oldValue = _yearsofExp;
                        _yearsofExp = value;

                        OnPropertyChanged("YearsofExp", oldValue, value);
                    }
                }
            }
        }  private int _yearsofExp = -1;



        /// <summary>
        /// Background of the Consultants
        /// </summary>
        public string Background
        {
            get { return _background; }
            set
            {
                if (_background != value)
                {
                    if (OnPropertyChanging("Background", _background, value))
                    {
                        string oldValue = _background;
                        _background = value;

                        OnPropertyChanged("Background", oldValue, value);
                    }
                }
            }
        }  private string _background = null;


        /// <summary>
        /// Keywords of the Consultants
        /// </summary>
        public string Keywords
        {
            get { return _keywords; }
            set
            {
                if (_keywords != value)
                {
                    if (OnPropertyChanging("Keywords", _keywords, value))
                    {
                        string oldValue = _keywords;
                        _keywords = value;

                        OnPropertyChanged("Keywords", oldValue, value);
                    }
                }
            }
        }  private string _keywords = null;


        /// <summary>
        /// Membership of the Consultants
        /// </summary>
        public string Membership
        {
            get { return _membership; }
            set
            {
                if (_membership != value)
                {
                    if (OnPropertyChanging("Membership", _membership, value))
                    {
                        string oldValue = _membership;
                        _membership = value;

                        OnPropertyChanged("Membership", oldValue, value);
                    }
                }
            }
        }  private string _membership = null;


        /// <summary>
        /// CurrentBillingCycle
        /// </summary>
        public string CurrentBillingCycle
        {
            get { return _currentBillingCycle; }
            set
            {
                if (_currentBillingCycle != value)
                {
                    if (OnPropertyChanging("CurrentBillingCycle", _currentBillingCycle, value))
                    {
                        string oldValue = _currentBillingCycle;
                        _currentBillingCycle = value;

                        OnPropertyChanged("CurrentBillingCycle", oldValue, value);
                    }
                }
            }
        }  private string _currentBillingCycle = null;


        /// <summary>
        /// True iff fees paid.
        /// </summary>
        public bool IsMembershipFeesGiven
        {
            get { return _isMembershipFeesGiven; }
            set
            {
                if (_isMembershipFeesGiven != value)
                {
                    if (OnPropertyChanging("IsMembershipFeesGiven", _isMembershipFeesGiven, value))
                    {
                        bool oldValue = _isMembershipFeesGiven;
                        _isMembershipFeesGiven = value;
                        OnPropertyChanged("IsMembershipFeesGiven", oldValue, value);
                    }
                }
            }
        }  private bool _isMembershipFeesGiven = false;


        /// <summary>
        /// MembershipStatus
        /// </summary>
        public string MembershipStatus
        {
            get { return _membershipStatus; }
            set
            {
                if (_membershipStatus != value)
                {
                    if (OnPropertyChanging("MembershipStatus", _membershipStatus, value))
                    {
                        string oldValue = _membershipStatus;
                        _membershipStatus = value;

                        OnPropertyChanged("MembershipStatus", oldValue, value);
                    }
                }
            }
        }  private string _membershipStatus = null;


        /// <summary>
        /// Date/time at which membership expires.
        /// </summary>
        public DateTime? MembershipExpiryDate
        {
            get { return _membershipExpiryDate; }
            set
            {
                if (_membershipExpiryDate != value)
                {
                    if (OnPropertyChanging("MembershipExpiryDate", _membershipExpiryDate, value))
                    {
                        DateTime? oldValue = _membershipExpiryDate;
                        _membershipExpiryDate = value;
                        OnPropertyChanged("MembershipExpiryDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _membershipExpiryDate = null;


        /// <summary>
        /// ProfileID of the Consultants
        /// </summary>
        public string ProfileID
        {
            get { return _profileID; }
            set
            {
                if (_profileID != value)
                {
                    if (OnPropertyChanging("ProfileID", _profileID, value))
                    {
                        string oldValue = _profileID;
                        _profileID = value;

                        OnPropertyChanged("ProfileID", oldValue, value);
                    }
                }
            }
        }  private string _profileID = null;


        /// <summary>
        /// Attachment of the Consultants
        /// </summary>
        public string Attachment
        {
            get { return _attachment; }
            set
            {
                if (_attachment != value)
                {
                    if (OnPropertyChanging("Attachment", _attachment, value))
                    {
                        string oldValue = _attachment;
                        _attachment = value;

                        OnPropertyChanged("Attachment", oldValue, value);
                    }
                }
            }
        }  private string _attachment = null;


        
        /// <summary>
        /// CBOPopMailAccount.
        /// </summary>
        public string CBOPopMailAccount
        {
            get { return _cbopopmailaccount; }
            set
            {
                if (_cbopopmailaccount != value)
                {
                    if (OnPropertyChanging("CBOPopMailAccount", _cbopopmailaccount, value))
                    {
                        string oldValue = _cbopopmailaccount;
                        _cbopopmailaccount = value;
                        OnPropertyChanged("CBOPopMailAccount", oldValue, value);
                    }
                }
            }
        }  private string _cbopopmailaccount = null;


        /// <summary>
        /// CBOPopMailPassword.
        /// </summary>
        public string CBOPopMailPassword
        {
            get { return _cbopopmailPassword; }
            set
            {
                if (_cbopopmailPassword != value)
                {
                    if (OnPropertyChanging("CBOPopMailPassword", _cbopopmailPassword, value))
                    {
                        string oldValue = _cbopopmailPassword;
                        _cbopopmailPassword = value;
                        OnPropertyChanged("CBOPopMailPassword", oldValue, value);
                    }
                }
            }
        }  private string _cbopopmailPassword = null;


        /// <summary>
        /// ProfileURL.
        /// </summary>
        public string ProfileURL
        {
            get { return _profileURL; }
            set
            {
                if (_profileURL != value)
                {
                    if (OnPropertyChanging("ProfileURL", _profileURL, value))
                    {
                        string oldValue = _profileURL;
                        _profileURL = value;
                        OnPropertyChanged("ProfileURL", oldValue, value);
                    }
                }
            }
        }  private string _profileURL = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty ConsultantsInformation object.
        /// </summary>
        public static ConsultantsInformation CreateNew()
        {
            ConsultantsInformation obj = new ConsultantsInformation();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static ConsultantsInformation CreateNew(ConsultantsInformationCollection collection)
        {
            ConsultantsInformation obj = CreateNew();
            
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetConsultantsInformationByID

        /// <summary>
        /// Returns an ConsultantsInformation object for the specified 'UserID' or null if no ConsultantsInformation with the specified parameter(s) exists.
        /// </summary>
        public static ConsultantsInformation GetConsultantsInformationByID(int userid)
        {
            ConsultantsInformationTableAdapter adapter = new ConsultantsInformationTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsConsultantsInformation.ConsultantsInformationDataTable tbl = adapter.GetData(userid);

            if (tbl.Rows.Count == 1)
            {
                ConsultantsInformation ConsultantsInformation = new ConsultantsInformation();
                RelationalToObject(ConsultantsInformation, tbl.Rows[0]);
                return (ConsultantsInformation);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'ConsultantsInformation' returned more than one User record.");

            return (null);
        }

        #endregion

        #region GetConsultantsInformationByProfileID

        /// <summary>
        /// Returns an ConsultantsInformation object for the specified 'UserID' or null if no ConsultantsInformation with the specified parameter(s) exists.
        /// </summary>
        public static ConsultantsInformation GetConsultantsInformationByProfileID(string Profileid)
        {
            ConsultantsInformationTableAdapter adapter = new ConsultantsInformationTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsConsultantsInformation.ConsultantsInformationDataTable tbl = adapter.GetDataByProfileID(Profileid);

            if (tbl.Rows.Count == 1)
            {
                ConsultantsInformation ConsultantsInformation = new ConsultantsInformation();
                RelationalToObject(ConsultantsInformation, tbl.Rows[0]);
                return (ConsultantsInformation);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'ConsultantsInformation' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified ConsultantsInformation.
        /// </summary>
        public static void Update(ConsultantsInformation obj)
        {
            bool updateConsultantsInformation = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateConsultantsInformation = (obj.ObjState != ObjectState.Unchanged);

                if (!updateConsultantsInformation)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsConsultantsInformation.ConsultantsInformationDataTable tbl = null;
            if (updateConsultantsInformation)
            {
                ConsultantsInformationTableAdapter adapter = new ConsultantsInformationTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsConsultantsInformation.ConsultantsInformationDataTable();
                dsConsultantsInformation.ConsultantsInformationRow row = tbl.NewConsultantsInformationRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("ConsultantsInformationID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("ConsultantsInformation: UserID={0}, ProfileID={1}, {2}", UserID, ProfileID));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more ConsultantsInformation objects.
    /// </summary>

    public class ConsultantsInformationCollection : List<ConsultantsInformation>
    {
        #region Types

        public class UserComparer : IComparer<ConsultantsInformation>
        {
            public int Compare(ConsultantsInformation c1, ConsultantsInformation c2)
            {
                return (String.Compare(c1.ProfileID, c2.ProfileID));
            }
        }

        #endregion

        #region Public Methods

        #region FindByConsultantsInformationID

        public ConsultantsInformation FindByUserID(int UserID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].UserID == UserID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindByProfileID

        public ConsultantsInformation FindByProfileID(string profileID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ProfileID == profileID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
